<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$product_code = isset($_POST["product_code"]) ? addslashes(trim($_POST["product_code"])) : '';
$product_color = isset($_POST["product_color"]) ? addslashes(trim($_POST["product_color"])) : '';
$core = new core();
if ($product_code == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền mã sản phẩm!"));
    die;
} else {
    $product = $core->getProductInfoByCode(addslashes($product_code));

    if ($product) {
        $color_photo_name = $product_color . "_" . $product->product_code . "_" . $product->manufacture;
        $color_photo_name = $core->convert_string_friendly($color_photo_name) . ".jpg";
        $path_color_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS . $color_photo_name;
        if (file_exists($path_color_photo)) {
            $src_photo_thumb = 'photo/extra_photo/thumb_extra_photo/' . $color_photo_name;
        } else {
            $src_photo_thumb = 'photo/thumb_photo/200_' . $product->main_photo;
        }
        $image = "<img src='" . $src_photo_thumb . "' width='200px' height='200px' id='product_thumb_image'>";
        if ($product->product_size != "") {
            $arr_size = explode(",", $product->product_size);
            $option_size = '';
            foreach ($arr_size as $size) {
                $option_size.="<option>" . $size . "</option>";
            }
        } else {
            $option_size = '';
        }
        if ($product->product_color != "") {
            $arr_color = explode(",", $product->product_color);
            $option_color = '';
            $option_color = "<option value=''>Chọn màu</option>";
            foreach ($arr_color as $color) {
                $option_color.="<option value='" . $color . "'>" . $color . "</option>";
            }
        } else {
            $option_color = '';
        }
        echo json_encode(array('type' => 0, 'product_id' => $product->product_id,
            'product_image' => $image,
            "product_size" => $option_size,
            'product_color' => $option_color,
            'product_price' => number_format($product->product_price / 1000),
            'store_price' => number_format($product->store_price),
            'stamp_price' => number_format($product->stamp_price),
            'product_code' => $product->product_code));
        die;
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Sản phẩm không có trong hệ thống!"));
        die;
    }
}
?>